/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.banglaocr.basic;

import com.googlecode.javacv.jna.cxcore.CvSize;
import com.googlecode.javacv.jna.cxcore.IplImage;
import static com.googlecode.javacv.jna.cv.*;
import static com.googlecode.javacv.jna.highgui.*;
/**
 *
 * @author admin
 */
public class SmoothImage {
    private String originalImagePath="";
    private IplImage originalImage = null;
    private IplImage smoothImage = null;
    private IplImage halfImage = null;
    private IplImage cannyImage = null;
    public SmoothImage(String imagePath) {
        this.originalImagePath = imagePath;

        this.originalImage = cvLoadImage(this.originalImagePath, 0);
    }

    public IplImage getSmoothImage() {
        if (this.originalImage==null) {
            return null;
        }
        if (this.smoothImage == null) {
            this.smoothImage = new IplImage();
            this.smoothImage = IplImage.createCompatible(this.originalImage);
            cvSmooth(this.originalImage, this.smoothImage, CV_GAUSSIAN, 3, 0, 0, 0);
//            this.smoothImage = this.originalImage;
        }

        return this.smoothImage;
    }

    public void saveSmoothImage(String pathAndName){
        this.getSmoothImage();
        cvSaveImage(pathAndName, smoothImage);
    }

    public void saveHalfImage(String pathName) {
        if (this.originalImage == null) {
            return;
        }

        if (this.halfImage == null) {
            CvSize.ByValue size = new CvSize.ByValue(this.originalImage.width/2, this.originalImage.height/2);
            this.halfImage = IplImage.create(size, this.originalImage.depth, this.originalImage.nChannels);
            cvPyrDown(this.originalImage, this.halfImage, CV_GAUSSIAN_5x5);
        }
        cvSaveImage(pathName, halfImage);
    }

    public void saveCannyImage(String pathName) {
        if (this.originalImage == null) {
            return;
        }

        if (this.cannyImage == null) {
//            CvSize.ByValue size = new CvSize.ByValue(this.originalImage.width/2, this.originalImage.height/2);
//            this.cannyImage = IplImage.create(this.originalImage.getCvSize(), this.originalImage.depth, this.originalImage.nChannels);
            this.cannyImage = IplImage.createFrom(this.originalImage.getBufferedImage());
//            this.cannyImage = IplImage.createCompatible(this.originalImage);
//            cvPyrDown(this.originalImage, this.cannyImage, CV_GAUSSIAN_5x5);
//            cvCvtColor(this.originalImage, this.cannyImage, CV_);
            cvCanny(this.cannyImage, this.cannyImage, 10, 100, 3);
//            cvSobel(this.cannyImage, this.cannyImage, 0, 1, 3);
        }
        cvSaveImage(pathName, this.cannyImage);
    }
}
